//
//  TKIMHeadView.m
//  TKIM_Thinkive
//
//  Created by liupm on 16/5/30.
//  Copyright © 2016年 liupm. All rights reserved.
//

#import "TKIMHeadView.h"
#import "TKIMLabel.h"

@interface TKIMHeadView()
@end

@implementation TKIMHeadView

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self drawView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self drawView];
    }
    return self;
}

-(void)drawView
{
    _font =  [UIFont boldSystemFontOfSize:15.0f];
    _textColor = [UIColor whiteColor];
    NSArray *arr = TKIMHeadViewColors;
    _placeLabelBackgroudColor = [TKIMColorHelper stringToColor:arr[0]];
    [self addSubview:self.placeLabel];
    [self addSubview:self.imageView];
}


#pragma mark 属性

-(UIImageView *)imageView
{
    if (!_imageView) {
        _imageView =  [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _imageView.aliCornerRadius = self.width/2.0;
        _imageView.contentMode = UIViewContentModeScaleAspectFill ;
    }
    return _imageView;
}


-(TKIMLabel *)placeLabel
{
    if (!_placeLabel) {
        _placeLabel = [[TKIMLabel alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _placeLabel.textAlignment = NSTextAlignmentCenter;
        _placeLabel.textColor = _textColor;
        _placeLabel.font = _font;
        _placeLabel.roundImageColor = _placeLabelBackgroudColor;
    }
    return _placeLabel;
}


-(void)setFont:(UIFont *)font
{
    _font = font;
    self.placeLabel.font = font;
}


-(void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;
    self.placeLabel.textColor = textColor;
}


-(void)setPlaceLabelBackgroudColor:(UIColor *)placeLabelBackgroudColor
{
    _placeLabelBackgroudColor = placeLabelBackgroudColor;
    self.placeLabel.backgroundColor = placeLabelBackgroudColor;
}


-(void)setPlaceText:(NSString *)placeText
{
    _placeText = placeText;
    self.placeLabel.text = placeText;
    if ([TKIMStringHelper isEmpty:placeText]) {
        if (self.placeLabel.superview) {
            [self.placeLabel removeFromSuperview];
        }
    }else{
        if (self.placeLabel && !self.placeLabel.superview) {
            [self addSubview:self.placeLabel];
        }
    }
}


-(void)setImage:(UIImage *)image
{
    _image = image;
    self.imageView.image = image;
    if(self.placeLabel && self.placeLabel.superview){
        [self.placeLabel removeFromSuperview];
    }
   
}

/**
 *  @Author lpm, 16-05-30 10:05:59
 *
 *  @brief  设置图片
 *
 *  @param imageUrl 图片地址
 *  @param placeText    占位文字
 */
-(void)setImageUrl:(NSString *)imageUrl placeText:(NSString *)placeText
{
    NSInteger colorIndex = [TKIMStringHelper parseCnToEn:placeText].length%5;
    NSArray *colorArr = TKIMHeadViewColors;
    self.placeLabel.roundImageColor = [TKIMColorHelper stringToColor:colorArr[colorIndex]];
    self.placeLabel.text = [TKIMHelper formatHeadName:placeText];
    if ([TKIMStringHelper isEmpty:imageUrl]) {
        if (self.placeLabel && !self.placeLabel.superview) {
            [self addSubview:self.placeLabel];
        }
        self.imageView.image = nil;
        return;
    }
    

    TKIMWS(weakSelf);
    NSURL *url = [NSURL URLWithString:imageUrl];
    [self.imageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        //如果头像存在则隐藏名字标签
        if (!error){
            if(weakSelf.placeLabel && weakSelf.placeLabel.superview){
                [weakSelf.placeLabel removeFromSuperview];
            }
        }else{

            if (weakSelf.placeLabel && !weakSelf.placeLabel.superview) {
                [weakSelf addSubview:weakSelf.placeLabel];
            }

        }
    }];

}

-(void)setImageUrl:(NSString *)imageUrl placeholderImage:(NSString*)imagename;
{
    TKIMWS(weakSelf);

    [weakSelf.placeLabel removeFromSuperview];
    [weakSelf.imageView removeFromSuperview];
    [weakSelf addSubview:self.imageView];
    NSURL *url = [NSURL URLWithString:imageUrl];
    [self.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:imagename]];
    
}

@end
